<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('coupons', function (Blueprint $table) {
            $table->id();
            $table->string('name')->comment('优惠券名称');
            $table->string('code')->unique()->comment('优惠券代码');
            $table->enum('type', ['fixed', 'percent', 'shipping', 'full_reduction'])
                ->default('fixed')
                ->comment('类型：fixed=固定金额, percent=百分比折扣, shipping=包邮, full_reduction=满减');
            $table->decimal('value', 10, 2)->comment('优惠金额/折扣百分比');
            $table->decimal('min_purchase', 10, 2)->default(0)->comment('最低消费金额，0表示无限制');
            $table->decimal('max_discount', 10, 2)->nullable()->comment('最大优惠金额，用于折扣券');
            $table->integer('total_quantity')->default(0)->comment('发放总数量，0表示无限制');
            $table->integer('used_quantity')->default(0)->comment('已使用数量');
            $table->integer('per_user_limit')->default(1)->comment('每人限领次数，0表示无限制');
            $table->integer('valid_days')->nullable()->comment('有效天数，从领取日期开始计算');
            $table->timestamp('start_time')->nullable()->comment('固定开始时间');
            $table->timestamp('end_time')->nullable()->comment('固定结束时间');
            $table->enum('applicable_type', ['all', 'category', 'product'])
                ->default('all')
                ->comment('适用范围：all=全场, category=指定分类, product=指定商品');
            $table->json('applicable_ids')->nullable()->comment('适用的分类/商品ID');
            $table->boolean('stackable')->default(false)->comment('是否可叠加使用');
            $table->text('description')->nullable()->comment('优惠券描述');
            $table->string('usage_note')->nullable()->comment('使用说明');
            $table->boolean('is_active')->default(true)->comment('是否启用');
            $table->boolean('is_new_user_only')->default(false)->comment('是否新用户专享');
            $table->integer('sort_order')->default(0)->comment('排序');
            $table->timestamps();
            
            $table->index('code');
            $table->index('type');
            $table->index('is_active');
            $table->index(['start_time', 'end_time']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('coupons');
    }
};
